package ar.edu.itba.it.cg.grupo01.game.terrain;

import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.BillboardNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.system.DisplaySystem;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;

public class Tree extends Node {

	private static final long serialVersionUID = 1L;

	public Tree(String name, PhysicsSpace physicsSpace, URL treeImage) {
		super(name);
		buildTree(physicsSpace, treeImage);
	}

	private void buildTree(PhysicsSpace physicsSpace, URL treeImage) {
		BlendState blendState = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		blendState.setBlendEnabled(true);
		blendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		blendState
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		blendState.setTestEnabled(true);
		blendState.setTestFunction(BlendState.TestFunction.GreaterThan);
		blendState.setEnabled(true);

		// set the blend equation between source and destination
		blendState.setBlendEquation(BlendState.BlendEquation.Add);
		blendState.setTestEnabled(false);

		Quad q = new Quad("Quad");
		// TextureState ts = DisplaySystem.getDisplaySystem().getRenderer()
		// .createTextureState();
		// ts.setEnabled(true);
		// Texture t = TextureManager.loadTexture(treeImage,
		// MinificationFilter.Trilinear, MagnificationFilter.Bilinear);
		//
		// ts.setTexture(t);

		// q.setRenderState(ts);
		q.setRenderState(blendState);
		q.updateRenderState();

		BillboardNode billboard = new BillboardNode("Billboard");
		billboard.setAlignment(BillboardNode.AXIAL);
		billboard.attachChild(q);
		billboard.setLocalScale(100f);

		this.attachChild(billboard);

		Box boxGeometry = new Box("post", new Vector3f(-1f, -10, -1f),
				new Vector3f(1f, 10, 1f));
		boxGeometry.setModelBound(new BoundingBox());
		boxGeometry.updateModelBound();

		BlendState blendState2 = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		blendState2.setBlendEnabled(true);
		blendState2.setSourceFunction(BlendState.SourceFunction.SourceAlpha);

		blendState2
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);

		blendState2.setTestEnabled(true);
		blendState2.setTestFunction(BlendState.TestFunction.Never);
		blendState2.setEnabled(true);

		boxGeometry.setRenderState(blendState2);
		boxGeometry.updateRenderState();

		final StaticPhysicsNode staticNode = physicsSpace.createStaticNode();
		staticNode.attachChild(boxGeometry);

		staticNode.setLocalScale(new Vector3f(1, 500, 1));
		staticNode.setLocalTranslation(-2.75f, 0, 3.25f);
		staticNode.updateGeometricState(0, true);

		this.attachChild(staticNode);

		staticNode.generatePhysicsGeometry();

	}

	public void setPosition(float x, float y, float z) {
		localTranslation.x = x;
		localTranslation.y = y;
		localTranslation.z = z;
	}

}
